package cn.t.repository.impl.user;


import cn.t.converter.user.CostCenterConverter;
import cn.t.core.mode.YesNoEnum;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.core.enums.base.BaseStatusEnum;
import cn.t.model.user.CostCenterDO;
import cn.t.persistence.entity.user.CostCenterPO;
import cn.t.persistence.mapper.user.CostCenterMapper;
import cn.t.repository.user.CostCenterRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 场景信息表数据仓库
 *
 * @author l
 */
@Slf4j
@Repository
@AllArgsConstructor
public class CostCenterRepositoryImpl extends RepositoryImpl<CostCenterMapper, CostCenterPO, CostCenterConverter, CostCenterDO> implements CostCenterRepository {


    @Override
    public List<CostCenterDO> findByModel(CostCenterDO costCenterDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ? " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<CostCenterPO> list = getWrapper(costCenterDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<CostCenterPO> getWrapper(CostCenterDO costCenterDO) {
        LambdaQueryChainWrapper<CostCenterPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(costCenterDO.getCostCenterCode())) {
            wrapper.like(CostCenterPO::getCostCenterCode, "%" + costCenterDO.getCostCenterCode().trim() + "%");
        }

        if (!ObjectUtils.isEmpty(costCenterDO.getCostCenterName())) {
            wrapper.like(CostCenterPO::getCostCenterName, "%" + costCenterDO.getCostCenterName().trim() + "%");
        }
        if (!ObjectUtils.isEmpty(costCenterDO.getCostCenterNameAll())) {
            wrapper.eq(CostCenterPO::getCostCenterName, costCenterDO.getCostCenterNameAll().trim() );
        }
        if (!ObjectUtils.isEmpty(costCenterDO.getFirmIds())) {
            wrapper.in(CostCenterPO::getFirmId, costCenterDO.getFirmIds());
        }

        if (!ObjectUtils.isEmpty(costCenterDO.getFirmId())) {
            wrapper.eq(CostCenterPO::getFirmId, costCenterDO.getFirmId());
        }
        if (!ObjectUtils.isEmpty(costCenterDO.getStatus())) {
            wrapper.eq(CostCenterPO::getStatus, costCenterDO.getStatus());
        } else {
            wrapper.eq(CostCenterPO::getStatus, String.valueOf(BaseStatusEnum.AVAILABLE.getCode()));
        }

        if (!ObjectUtils.isEmpty(costCenterDO.getIsDeleted())) {
            wrapper.eq(CostCenterPO::getIsDeleted, costCenterDO.getIsDeleted());
        } else {
            wrapper.eq(CostCenterPO::getIsDeleted, YesNoEnum.FALSE.getCode());
        }

        if (!ObjectUtils.isEmpty(costCenterDO.getGmtCreatedStart()) && !ObjectUtils.isEmpty(costCenterDO.getGmtCreatedEnd())) {
            wrapper.between(CostCenterPO::getGmtCreated, costCenterDO.getGmtCreatedStart(), costCenterDO.getGmtCreatedEnd());
        }
        if (!ObjectUtils.isEmpty(costCenterDO.getGmtModifiedStart()) && !ObjectUtils.isEmpty(costCenterDO.getGmtModifiedEnd())) {
            wrapper.between(CostCenterPO::getGmtModified, costCenterDO.getGmtModifiedStart(), costCenterDO.getGmtModifiedEnd());
        }
        wrapper.in(!CollectionUtils.isEmpty(costCenterDO.getCostCenterIds()), CostCenterPO::getId, costCenterDO.getCostCenterIds());
        return wrapper;
    }

    @Override
    public long count(CostCenterDO costCenterDO) {
        return getWrapper(costCenterDO).count();
    }

    @Override
    public List<CostCenterDO> getCostCenterList(List<Long> ids) {
        LambdaQueryChainWrapper<CostCenterPO> wrapper1 = lambdaQuery();
       return converter.po2doList( wrapper1.in(ObjectUtils.isNotEmpty(ids), CostCenterPO::getId, ids).list());
    }
}
